const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const router = new Router();

// 最新上线
router.post('/zd/getNewList', async ctx => {
     let usr = ctx.user
    let tenantId = usr.tenantId;
    let goodsql = `
            select top 20 a.id,a.spec,a.unit1,a.unit2,a.name,a.imgUrl,iif(b.price is null,isnull(a.price2,0),b.price) price,c.sendInvId,c.buyQty,c.sendQty from v_goods a
            left join (select * from v_price_his_last where tenantId=${tenantId} and buId=${usr.buId}) b on a.tenantId=b.tenantId and a.id=b.invId and a.unit1=b.unit
            left join (select * from v_send_last where tenantId=${tenantId} and buId=${usr.buId}) c on a.tenantId=c.tenantId and a.id=c.invId
            where a.tenantId=${usr.tenantId} order by a.ctime desc
        `
    let recList = await db.query(goodsql, {type: QueryTypes.SELECT})
    recList.map(row => {
        row.send = ''
        if(row.buyQty) {
            row.send = `买${row.buyQty}赠${row.sendQty}`
        }
    })

    return ctx.suc('', {
        list: recList
    })
})

module.exports = router;
